/*
 * @(#)ListWorkflowTrackingAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.staff.bean.StaffRecord;
import com.dcivision.staff.dao.StaffRecordDAObject;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
 * <p>Class Name:       ListWorkflowTrackingAction.java    </p>
 * <p>Description:      The list action class for ListWorkflowTracking.jsp</p>
 * @author           Angus Shiu
 * @company          DCIVision Limited
 * @creation date    11/12/2003
 * @version          $Revision: 1.9.2.1 $
 */
 
public class ListWorkflowTrackingAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.9.2.1 $";

  
/*
 * (non-Javadoc)
 * @see com.dcivision.framework.web.AbstractListAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    String searchField = ((AbstractSearchForm)form).getBasicSearchField();
    String searchKey = ((AbstractSearchForm)form).getBasicSearchKeyword();
    if(!Utility.isEmpty(searchField)
        &&searchField.trim().equalsIgnoreCase("A.TRACK_ID")
        &&!Utility.isEmpty(searchKey)
        &&searchKey.startsWith("0")){
      
      try {
        ((AbstractSearchForm)form).setBasicSearchKeyword(Integer.valueOf(searchKey).toString());
      }
      catch (NumberFormatException e) {
        log.error(e.toString(),e);
      }
      
    }
    return super.execute(mapping, form, request, response);
  }

  /** Creates a new instance of ListWorkflowTrackingAction */
  public ListWorkflowTrackingAction() {
    super();
    this.setListName("workflowTrackingList");
    this.setListFunctionName("getListTrackingByUserRecordID");
  }
  
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_TRACK);
  }
  
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowProgressDAObject");
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    ListWorkflowProgressForm listWorkflowProgressForm = (ListWorkflowProgressForm)form;
    String extendTitle="";

    boolean isShowFullList = GlobalConstant.TRUE.equals(listWorkflowProgressForm.getShowFullList());

    if(isShowFullList){
      //title is showPending List
      extendTitle = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.full_list");
    }else{
      extendTitle = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "workflow.label.pending_list");
    }
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
  }
  
  /**
   *  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping, form, request, response);
    
    UserRecordDAObject userDAO = (UserRecordDAObject) this.getDAObjectByClassName(request, "com.dcivision.user.dao.UserRecordDAObject");
    StaffRecordDAObject staffDAO = (StaffRecordDAObject) this.getDAObjectByClassName(request, "com.dcivision.staff.dao.StaffRecordDAObject");
    
    UserRecord currentUser = this.getSessionContainer(request).getUserRecord();
    StaffRecord currentStaff = (StaffRecord) staffDAO.getObjectByID(currentUser.getUserLinkID());
    Integer hierarchyLevel = SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.STAFF_HIERARCHY_LEVEL);
    Integer levelNo = null, hierarchyID = null;
    try {
      for (int i=1; i<=hierarchyLevel.intValue(); i++) {
        Method mth = currentStaff.getClass().getMethod("getLevel"+i+"ID" , null);
        if (mth.invoke(currentStaff, null)==null) {
          break;
        } else {
          levelNo = new Integer(i);
          hierarchyID = (Integer) mth.invoke(currentStaff, null);
        }
      }
    } catch (java.lang.NoSuchMethodException e) {
      throw new ApplicationException(e);
    } catch (java.lang.IllegalAccessException e) {
      throw new ApplicationException(e);
    } catch (java.lang.reflect.InvocationTargetException e) {
      throw new ApplicationException(e);
    }
    
    //get the report to me's user record list
    List reportToMeStaffList = staffDAO.getStaffRecordListByReportToStaffRecordID(currentStaff.getID());
    List reportToMeUserRecordList = userDAO.getListByStaffRecordList(reportToMeStaffList);
    List subordinateList = levelNo==null? new ArrayList(): userDAO.getUnderlingListByStaffLevelNo(levelNo, hierarchyID);
    
    
    
    request.setAttribute("subordinateList", subordinateList);
    
    request.setAttribute("reportToMeUserRecordList", reportToMeUserRecordList);
    
  }

}